package com.net.ip;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.net.ip.vo.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Neighborhood extends ActionSupport implements Action ,ServletRequestAware{

	static 	private  List<NeighborhoodTabVo>  neighborhoodList;
	private	HttpServletRequest request;
	static	NeighborhoodTabVo tabvo = null;


	
	public static void main(String[] args) throws InterruptedException {
//		System.out.println(Neighborhood.getNeighborhood("net view /domain", ""));
//		System.out.println(Neighborhood.getNeighborhood());
		Neighborhood.getNeighborhood();
	}
	
	
	public void test() throws InterruptedException{
		
		NeighborhoodTabVo tabvo = null;
		for (int i = 0; i <= 10; i++ ) {
			tabvo = new NeighborhoodTabVo();
			tabvo.setName("zhangsan");
			tabvo.setIpv4("192.168.1.115");
			tabvo.setMask("255.255.255.0");
			tabvo.setGetway("192.168.1.1");
			neighborhoodList.add(tabvo);
		}
		
		
	}
	public   static String getNeighborhood() throws InterruptedException {
				
		neighborhoodList = new ArrayList<NeighborhoodTabVo>();
		
		get_Neighborhood("net view /domain", "");
				
//		test();
		
		for (NeighborhoodTabVo tab : neighborhoodList) {
			System.out.println(tab.getName());
			System.out.println(tab.getIpv4());
		}

		return SUCCESS;
	}

	
	private  static  void get_Neighborhood(String command, String domain) {
		
		String line = "";
		boolean begin = false;
		StringBuffer sb = new StringBuffer(); // 可变长
		try {
			Process process = Runtime.getRuntime().exec(command); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			while ((line = br.readLine()) != null) {
				line = line.trim();
				// System.out.println(line);
				// 你的工作组千万不要命名为“命令成功完成。”或者是“The command completed successfully.”
				if (line.endsWith("命令成功完成。")
						|| line
								.equalsIgnoreCase("The command completed successfully.")) {
					begin = false;
					break;
				}
				if (begin) {
					if (domain.length() > 0 && line.length() >= 2) {
						if (line.indexOf(" ") != -1)
							line = line.substring(2, line.indexOf(" "));
						else
							line = line.substring(2, line.length());
						
						String temp = getNeighborIP(line);
						if (temp != null) {
							
							tabvo = new NeighborhoodTabVo();
							tabvo.setName(line);
							tabvo.setIpv4(temp);
							tabvo.setMask("12222");
							tabvo.setGetway("djdldld");
							neighborhoodList.add(tabvo);
							
							line = line + " " + temp;
							sb.append("  ").append(line).append("\r\n");
						}
					} else {
						 sb.append(line).append("\r\n");
						get_Neighborhood("net view /domain:" + line,
								line);
					}
				}
				if (line.endsWith("-")) {
					begin = true;
				}

			}
			br.close();
			process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。
		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
		//return sb.toString();
	}

	
	/*
	private static String getNeighborhood() {
		
		String line = "";
		boolean begin = false;
		StringBuffer sb = new StringBuffer(); // 可变长
		
		try {
			Process process = Runtime.getRuntime().exec(command); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			while ((line = br.readLine()) != null) {
				line = line.trim();
				// System.out.println(line);
				// 你的工作组千万不要命名为“命令成功完成。”或者是“The command completed successfully.”
				if (line.endsWith("命令成功完成。")
						|| line
								.equalsIgnoreCase("The command completed successfully.")) {
					begin = false;
					break;
				}
				if (begin) {
					if (domain.length() > 0 && line.length() >= 2) {
						if (line.indexOf(" ") != -1)
							line = line.substring(2, line.indexOf(" "));
						else
							line = line.substring(2, line.length());
						
						String temp = getNeighborIP(line);
						if (temp != null) {
							line = line + " " + temp;
							sb.append("  ").append(line).append("\r\n");
						}
					} else {
						 sb.append(line).append("\r\n");
						 command = "net view /domain:" + line;
						 domain = line;
						sb.append(getNeighborhood());
					}
				}
				if (line.endsWith("-")) {
					begin = true;
				}

			}
			br.close();
			process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。
		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
		return sb.toString();
	}

	
	*/
	public static String getNeighborIP(String user) {

		String userIP = null;

		try {

			Process process = Runtime.getRuntime().exec("Ping " + user + " -4"); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));

			userIP = br.readLine();
			userIP = br.readLine();
			
			if (userIP != null) { // 可以连接
				userIP.getBytes("UTF-8");
				userIP = userIP.substring(userIP.indexOf("[") + 1, userIP
						.indexOf("]"));
			} else {
				userIP = null;
			}

			br.close();
			process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。

		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
//		System.out.println(user + "'s ip :" + userIP);
		return userIP;

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	/*
	public String  getNeighborhood() {		
			neighborhoodList = new ArrayList<NeighborhoodTabVo>();
			int i;
			NeighborhoodTabVo tabvo = null;
			for (i = 0; i <= 10; i++ ) {
				tabvo = new NeighborhoodTabVo();
				tabvo.setName("zhangsan");
				tabvo.setIpv4("10.21.23.12");
				tabvo.setMask("jjjjjj");
				tabvo.setGetway("123333");
				neighborhoodList.add(tabvo);
			}
				
			
			return SUCCESS;
		}
	*/
	

	public List<NeighborhoodTabVo> getNeighborhoodList() {
		return neighborhoodList;
	}

	public void setNeighborhoodList(List<NeighborhoodTabVo> neighborhoodList) {
		this.neighborhoodList = neighborhoodList;
	}

}